package com.rtssoft.scalatest

import scala.math._


object TestSomeImplicits {

    implicit def int2wrapper(i: Double) = new Powerizer(i)

    class Powerizer(x : Double) {
        def **(p : Double): Double = pow(x, p)
        def ! = factor(x.toInt)
        def factor(n: BigInt): BigInt = if (n==0) 1 else n * factor(n-1)
    }

    def test(x: Double) = {
        println("POWER IS : " + (x**2))
        println("FACTORIAL IS : " + (x!))
    }


    /**
     * Derivation example
     */
    def derivExample() : Unit = {
        
        def D(f:Double => Double, dx:Double = 0.00000001) = (x:Double) => (f(x + dx) - f(x)) / dx

        val cos = D(sin)

        val f1 = (x:Double) => x**2

        println("sin(pi/4)=" + sin(Pi / 4))
        println("sin'(pi/4)=" + cos(Pi / 4))
        println("sin'(pi/4)=" + D(sin)(Pi / 4))
        println("(x^2)'(2)=" + D((x:Double)=>x**2)(2))
        println("(x^3)'(2)=" + D((x:Double)=>x**3)(2))
    }

}
